//
//  HXCustomVideoVC.m
//  ZCooking
//
//  Created by 韩旭 on 16/9/8.
//  Copyright © 2016年 HX_. All rights reserved.
//

#import "HXCustomVideoVC.h"
#import "HX_Macro.h"
#import "HXCustomVideoModel.h"
#import "UIImage+HXImageColor.h"
#import "HXDiscountTCell.h"
#import "HXTagsModel.h"
#import "HXDishesModel.h"
#import "HXDiscountMoreTCell.h"

@interface HXCustomVideoVC ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) NSMutableArray *string;
@property (nonatomic, strong) UIButton *leftBtn;
@property (nonatomic, strong) UILabel *labelTitle;
@property (nonatomic, strong) UIButton *searchBtn;
@property (nonatomic, strong) UIButton *shareBtn;
@property (nonatomic, strong) UIView *navgationView;
@property (nonatomic, strong) UIImageView *imageHeader;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *arrAll;
@property (nonatomic, strong) UIButton *sear;
@end
#define kTableView @"HXDiscountTCell"
@implementation HXCustomVideoVC
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    self.navigationController.navigationBarHidden = YES;
}
- (NSMutableArray *)arrAll {
    if (!_arrAll) {
        _arrAll = [NSMutableArray array];
    }
    return _arrAll;
}
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:0];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableHeaderView = self.imageHeader;
        _tableView.separatorStyle = NO;
        [_tableView registerClass:[HXDiscountMoreTCell class] forCellReuseIdentifier:@"HXDiscountMoreTCell"];
        [self.view addSubview:_tableView];
    }
    return _tableView;
}
- (NSMutableArray *)string {
    if (!_string) {
        _string = [ NSMutableArray array];
    }
    return _string;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    if (self.netId) {
        NSArray *arr = [self.netId componentsSeparatedByString:@"#"];
        for (NSString *str in arr) {
            [self.string addObject:str];
        }
        self.ID = [self.string[1] integerValue];
    }
    [self getNewWorking];
    [self creatHeadView];
    [self tableView];
    [self creatNavgationView];
}
- (void)creatHeadView {
    self.imageHeader = [[UIImageView alloc] init];
    _imageHeader.HX_height = 200;
    _imageHeader.layer.masksToBounds = YES;
    _imageHeader.contentMode = UIViewContentModeScaleAspectFill;
    _imageHeader.HX_width = SCREEN_WIDTH;
    [self.view addSubview:_imageHeader];
}
- (void)creatNavgationView {
    self.navgationView = [[UIView alloc] init];
    _navgationView.HX_width = SCREEN_WIDTH;
    _navgationView.HX_height = 69;
    _navgationView.HX_x = 0;
    _navgationView.HX_y = 0;
    [self.view addSubview:_navgationView];
    self.leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.leftBtn setImage:[UIImage imageNamed:@"xiangzuoBack1"] forState:UIControlStateNormal];
    _leftBtn.centerY = _navgationView.centerY;
    _leftBtn.HX_x = 20;
    _leftBtn.HX_width = 30;
    _leftBtn.HX_height = 20;
    [self.navgationView addSubview:_leftBtn];
    [self.leftBtn addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    self.searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.searchBtn.HX_width = 30;
    self.searchBtn.HX_height = 30;
    self.searchBtn.HX_x = self.navgationView.HX_width * 0.75;
    [self.searchBtn setImage:[UIImage imageNamed:@"searchWhite"] forState:UIControlStateNormal];
    self.searchBtn.centerY = self.leftBtn.centerY;
    [self.navgationView addSubview:_searchBtn];
    self.shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.shareBtn setImage:[UIImage imageNamed:@"fenxiang"] forState:UIControlStateNormal];
    self.shareBtn.HX_width = 30;
    self.shareBtn.HX_height = 30;
    self.shareBtn.HX_x = self.searchBtn.HX_x + self.searchBtn.HX_width + 20;
    self.shareBtn.centerY = self.searchBtn.centerY;
    [self.navgationView addSubview:_shareBtn];
}
- (void)creatTitleLable {
    self.labelTitle = [[UILabel alloc] init];
    self.labelTitle.text = self.title;
    self.labelTitle.textColor = [UIColor whiteColor];
    self.labelTitle.HX_width = [SelfSizeTools selfSizeHeightWithString:_labelTitle.text height:40 Font:[UIFont systemFontOfSize:17]];
    self.labelTitle.HX_height = self.navgationView.HX_height;
    self.labelTitle.centerX = self.navgationView.centerX;
    self.labelTitle.centerY = self.leftBtn.centerY;
    [self.navgationView addSubview:_labelTitle];
}
- (void)back:(UIButton *)btn {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)getNewWorking {
    NSString *url = @"http://api.izhangchu.com/";
      NSString *pathBody = [NSString stringWithFormat:@"scene_id=%ld&page=1&methodName=SceneInfo&size=20&version=4.40&", self.ID];
    NSString *body = pathBody;
    NSDictionary *dicHeader = @{@"Content-Type" : @"application/x-www-form-urlencoded; charset=UTF-8"};
    [HXNetTool postNetWithUrl:url body:body BodyStyle:HXBodyString Header:dicHeader Response:HXJSON Success:^(id result) {
        NSDictionary *results = result;
        NSDictionary *dicData = results[@"data"];
        HXCustomVideoModel *video = [HXCustomVideoModel ModelWithDic:dicData];
        [self.imageHeader sd_setImageWithURL:[NSURL URLWithString:video.scene_background]];
        self.title = video.scene_title;
        [self creatTitleLable];
        [self.arrAll addObject:video];
        [_tableView reloadData];
        [self.string removeAllObjects];
    } Failure:^(NSError *error) {
    }];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
      HXCustomVideoModel *videoModle = [self.arrAll firstObject];
    if (section == 0) {
        return videoModle.dishes_list.count;
    } else {
        return 1;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     HXCustomVideoModel *videoModle = [self.arrAll firstObject];
    if (indexPath.section == 0) {
        static NSString *ID = @"DD";
        HXDiscountTCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        if (!cell) {
            NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"HXDiscountTCell" owner:nil options:nil];
            cell = [nibs lastObject];
        }
        cell.selectionStyle = NO;
        cell.dishesModel = videoModle.dishes_list[indexPath.row];
        
        cell.arrBtns = [videoModle.dishes_list[indexPath.row] tags_info];
        return cell;
    } else {
        HXDiscountMoreTCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HXDiscountMoreTCell"];
        cell.relateArr = videoModle.relates;
        cell.backgroundColor = COLORAUTO;
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    } else {
        return 50;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 1) {
        UIView *viewTitle = [[UIView alloc] init];
        UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, viewTitle.HX_height)];
        lable.text = @"查看更多相关内容";
         lable.centerY = viewTitle.centerY;
         lable.HX_width = [SelfSizeTools selfSizeHeightWithString:lable.text height:30 Font:[UIFont systemFontOfSize:17]];
        lable.centerX = SCREEN_WIDTH / 2;
        lable.HX_height = 50;
        [viewTitle addSubview:lable];
        UIImageView *img = [[UIImageView alloc] init];
        img.centerY = lable.centerY;
        img.frame = CGRectMake(lable.HX_width + lable.HX_x + 30, lable.HX_y, 30, 30);
        img.image = [UIImage imageNamed:@"xiangyoutioazhuan"];
        [viewTitle addSubview:img];
        
        return viewTitle;
    } else {
        return nil;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 120;
    } else {
        return 200;
    }
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat offset_y = scrollView.contentOffset.y;
    CGFloat alpha = (offset_y + 20) / 200.0f;
    CGFloat colorF = 1 - alpha;
    UIImageView *ima = [[UIImageView alloc] init];
    ima.image = [UIImage imageNamed:@"search"];
    ima.image = [ima.image imageMaskWithColor:[UIColor colorWithWhite:colorF alpha:1.000]];
    [self.searchBtn setImage:ima.image forState:UIControlStateNormal];
    self.navgationView.backgroundColor = [UIColor colorWithWhite:0.955 alpha:alpha];
    UIImageView *imgLeft = [[UIImageView alloc] init];
    imgLeft.image = [UIImage imageNamed:@"xiangzuoBack1"];
    imgLeft.image = [imgLeft.image imageMaskWithColor:[UIColor colorWithWhite:colorF alpha:1.000]];
    [self.leftBtn setImage:imgLeft.image forState:UIControlStateNormal];
    UIImageView *imageShare = [[UIImageView alloc] init];
    imageShare.image = [UIImage imageNamed:@"fenxiang"];
    imageShare.image = [imageShare.image imageMaskWithColor:[UIColor colorWithWhite:colorF alpha:1.000]];
    [self.shareBtn setImage:imageShare.image forState:UIControlStateNormal];
    self.labelTitle.textColor = [UIColor colorWithWhite:colorF alpha:1.000];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
